package chess.utils;

import java.util.StringTokenizer;

/**
 * Parser rozpoznavajici dulezite hodnoty ve style12 informaci
 * 
 * @author dove
 * 
 */
public class Style12 {

	private String line;

	private String[] words = new String[50];

	private String[][] positions = new String[8][8];
	
	private String[][] positions2 = new String[8][8];

	private String positionsString = "";

	private int[] reverse2 = { 7, 6, 5, 4, 3, 2, 1, 0 };

	/**
	 * Inicializuje parser
	 * 
	 * @param line
	 *            radek obsahujici style12 informace zacinajici <12>
	 */
	public Style12(String line) {
		this.line = line;
		parse();
	}

	public Style12(String name1, String name2, String materialStrenght1,
			String materialStrenght2, int time1, int time2, boolean flip) {
		words[8] = name1;
		words[9] = name2;
		words[13] = materialStrenght1;
		words[14] = materialStrenght2;
		words[15] = String.valueOf(time1);
		words[16] = String.valueOf(time2);
		if (flip) {
			words[21] = "0";
		} else {
			words[21] = "1";
		}

	}

	/**
	 * Uklada pozice figur a nasledujici informace style12 do poli
	 */
	private void parse() {
		positionsString = line.substring(5, 76);

		StringTokenizer token = new StringTokenizer(line);

		String pom = token.nextToken();
		for (int i = 0; i < 8; i++) {
			pom = token.nextToken();
			for (int j = 0; j < 8; j++) {
				positions[j][i] = String.valueOf(pom.charAt(j));

			}
		}
		ChessBoardParser.printArray(positions);
		for (int i = 0; i < 24; i++) {
			words[i] = token.nextToken();
		}
		if (words[21].equals("1")) {
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					this.positions2[i][j] = positions[i][reverse2[j]];
				}
			}
			this.positions=positions2;
//			System.out.println();
//			ChessBoardParser.printArray(positions);
		}
		
//		System.out.println();

	}

	/**
	 * Vraci pozice figur
	 * 
	 * @return pole s pozicemi
	 */
	public String[][] getBoardPositions() {
		return positions;
	}

	public String getPositionsString() {
		return positionsString;
	}

	/**
	 * Barva figur hrace na tahu
	 * 
	 * @return barva na tahu
	 */
	public String getColor() {
		return words[0];
	}

	public int getGameID() {
		return Integer.valueOf(words[7]);
	}

	/**
	 * Vraci jmeno prvniho hrace
	 * 
	 * @return jmeno 1 hrace
	 */
	public String getName1() {
		return words[8];
	}

	/**
	 * Vraci jmeno druheho hrace
	 * 
	 * @return jmeno 2 hrace
	 */
	public String getName2() {
		return words[9];
	}

	/**
	 * Vraci silu figur prvniho hrace
	 * 
	 * @return
	 */
	public String getMaterialStrength1() {
		return words[13];
	}

	/**
	 * Vraci silu figur druheho hrace
	 * 
	 * @return
	 */
	public String getMaterialStrength2() {
		return words[14];
	}

	/**
	 * Vraci cas prvniho hrace
	 * 
	 * @return
	 */
	public int getTime1() {
		return Integer.valueOf(words[15]);
	}

	/**
	 * Vraci cas druheho hrace
	 * 
	 * @return
	 */
	public int getTime2() {
		return Integer.valueOf(words[16]);
	}

	/**
	 * Vraci predchozi tah
	 * 
	 * @return
	 */
	public String getMove() {
		return words[18];
	}

	/**
	 * Vraci orientaci sachovnice
	 * 
	 * @return true jestlize bile figury dole, false jestlize cerne figury dole
	 */
	public boolean getFlip() {
		if (words[21].equals("0")) {
			return true;
		} else {
			return false;
		}
	}
}
